Private VSTwain1 As New VintaSoftTwain()
Private _isScanFinished As Boolean
Private _step As Integer
Private _imageCount As Integer
''' <summary>
''' This method scans the first image, stops the document feeder,
''' scans the second image.
''' </summary>
Private Sub ScanImage_StopFeeder_ScanImage()
_isScanFinished = False
_step = 0
_imageCount = 0
VSTwain1.DeviceManager_IsTwain2Compatible = True
' open the device manager
If Not VSTwain1.DeviceManager_Open() Then
Console.WriteLine(VSTwain1.errorString)
Exit Sub
End If
' get the device index
Dim deviceName As String = "KODAK Scanner: i5000"
Dim deviceIndex As Integer = VSTwain1.DeviceManager_FindDevice(deviceName)
If deviceIndex = -1 Then
Throw New ApplicationException(String.Format("Device '{0}' is not found.", deviceName))
End If
' select the device by index
VSTwain1.DeviceManager_SelectedDeviceIndex = deviceIndex
' disable device UI
VSTwain1.Device_ShowUI = False
' specify that device must be closed after scan
VSTwain1.Device_DisableAfterAcquire = True
' open the device
If Not VSTwain1.Device_Open Then
Console.WriteLine(VSTwain1.errorString)
Exit Sub
End If
' specify that 2 images must be acquired from scanner
VSTwain1.Device_XferCount = 2
' subscribe to the device events
AddHandler VSTwain1.DeviceImageAcquired, AddressOf VSTwain1_ImageAcquired
AddHandler VSTwain1.DeviceScanCompleted, AddressOf VSTwain1_ScanCompleted
AddHandler VSTwain1.DeviceScanFailed, AddressOf VSTwain1_ScanFailed
AddHandler VSTwain1.DeviceScanCanceled, AddressOf VSTwain1_ScanCanceled
' run asynchronous image acqusition
VSTwain1.Device_AcquireImage()
' wait while feeder will be stopped
While Not _isScanFinished
Application.DoEvents()
End While
If _imageCount <> 1 Then
Throw New ApplicationException("Wrong acquired image count.")
End If
If VSTwain1.Device_State <> DEVICESTATE.DEVICESTATE_DeviceIsReadyToTransferImage Then
Throw New ApplicationException("Feeder is NOT stopped.")
End If
Console.WriteLine("Feeder is stopped.")
_isScanFinished = False
' continue asynchronous image acqusition
VSTwain1.Device_AcquireImage()
' wait while image scan will be finished
While Not _isScanFinished
Application.DoEvents()
End While
If _imageCount <> 2 Then
Throw New ApplicationException("Wrong acquired image count.")
End If
' unsubscribe from device events
RemoveHandler VSTwain1.DeviceImageAcquired, AddressOf VSTwain1_ImageAcquired
RemoveHandler VSTwain1.DeviceScanCompleted, AddressOf VSTwain1_ScanCompleted
RemoveHandler VSTwain1.DeviceScanFailed, AddressOf VSTwain1_ScanFailed
RemoveHandler VSTwain1.DeviceScanCanceled, AddressOf VSTwain1_ScanCanceled
' close the device
VSTwain1.Device_Close()
End Sub
Private Sub VSTwain1_ImageAcquired()
Console.WriteLine("Image is acquired.")
' increment image count
_imageCount += 1
' if we need stop the feeder
If _step = 0 Then
' stop the feeder
VSTwain1.Device_Feeder_Stop()
' specify that feeder is stopped
_step = 1
_isScanFinished = True
End If
End Sub
Private Sub VSTwain1_ScanCompleted()
Console.WriteLine("Scan is completed.")
_isScanFinished = True
End Sub
Private Sub VSTwain1_ScanFailed(errorString As String)
Console.WriteLine(String.Format("Scan is failed: {0}.", errorString))
_isScanFinished = True
End Sub
Private Sub VSTwain1_ScanCanceled()
Console.WriteLine("Scan is canceled.")
_isScanFinished = True
End Sub